package com.newlink.building.activities.main.dialer.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.newlink.building.BR
import com.newlink.building.NL_App
import com.newlink.building.R
import com.newlink.building.activities.main.history.data.Module_Phone_GroupedCallLogData

/**
 * @author wangyang
 * @date 2022/11/18
 * @description 拨号页面-拨号搜索列表的adapter
 */
class Module_Phone_DialerSearchAdapter(
    var context: Context,
    var data: ArrayList<Module_Phone_GroupedCallLogData>
) :
    RecyclerView.Adapter<Module_Phone_DialerSearchAdapter.BaseViewHolder>() {

    fun refreshData(newData: ArrayList<Module_Phone_GroupedCallLogData>) {
        this.data = newData
        this.notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        return BaseViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent?.context),
                viewType,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        var binding = holder.dataBinding
        binding.setVariable(BR.callLogData, data[position])

        holder.itemView.setOnClickListener {
            NL_App.coreContext.startCall(data[position].lastCallLogData.getSipLine())
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun getItemViewType(position: Int): Int {
        return R.layout.item_dialer_history
    }

    inner class BaseViewHolder(var dataBinding: ViewDataBinding) :
        RecyclerView.ViewHolder(dataBinding.root)
}
